package com.gpj.learn.netty.nio.chat;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

/**
 * @className SocketUtil
 * @description TODO
 * @author GPJ
 * @date 2020/2/17 10:35
 * @version 1.0
 **/
public class SocketUtil {
    private static ByteBuffer buffer = ByteBuffer.allocate(1024);
    
    public static byte[] read(ReadableByteChannel channel) throws IOException {
        int read = channel.read(buffer);
        buffer.flip();

        byte[] bytes = new byte[read];
        buffer.get(bytes, 0 , read);

        buffer.clear();
        return bytes;
    }

    public static void write(WritableByteChannel channel, byte[] bytes) throws IOException {
        buffer.put(bytes);

        buffer.flip();

        channel.write(buffer);
    }
    
}
